<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8'>
    <meta content='width=device-width, initial-scale=1.0' name='viewport'>
    <meta content='' name='description'>
    <meta content='Nils Nordman' name='author'>
    <link href='/images/howl.png' rel='shortcut icon'>
    <title>Howl :: Spec - howl.Settings</title>
    <link href="/stylesheets/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/syntax.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/howl.css" media="screen" rel="stylesheet" type="text/css" />
    <link href='//fonts.googleapis.com/css?family=Josefin+Slab' rel='stylesheet' type='text/css'>
    <link href='//fonts.googleapis.com/css?family=Open+Sans+Condensed:700' rel='stylesheet' type='text/css'>
  </head>
  <body class='doc doc_spec doc_spec_settings_spec'>
    <div class='container'>
      <div class='masthead'>
        <ul class='nav nav-pills'>
          <li>
            <a href='/'>
              <span class='glyphicon glyphicon-home'></span>
              Home
            </a>
          </li>
          <li>
            <a href='/doc/'>
              <span class='glyphicon glyphicon-book'></span>
              Documentation
            </a>
          </li>
          <li>
            <a href='/blog/'>
              <span class='glyphicon glyphicon-bullhorn'></span>
              Blog
            </a>
          </li>
          <li>
            <a href='/contact.html'>
              <span class='glyphicon glyphicon-inbox'></span>
              Contact
            </a>
          </li>
        </ul>
      </div>
      <ol class="breadcrumb"><li><a href="/">Home</a></li><li><a href='../'>Howl 0.3 Documentation</a></li><li>Spec</li><li>Spec - howl.Settings</li></ol>
      <div class="spec-group spec-group-1">&#x000A;&#x000A;<h1 id="howl.settings">howl.Settings</h1>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="kd">local</span><span class="w"> </span><span class="n">tmpdir</span><span class="p">,</span><span class="w"> </span><span class="n">settings</span><span class="w">&#x000A;&#x000A;</span><span class="n">before_each</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">tmpdir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">tmpdir</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">settings</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Settings</span><span class="w"> </span><span class="n">tmpdir</span><span class="w">&#x000A;&#x000A;</span><span class="n">after_each</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">tmpdir</span><span class="o">\</span><span class="n">rm_r</span><span class="o">!</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id=".dir-is-set-to-the-settings-directory-if-available">.dir is set to the settings directory if available</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">tmpdir</span><span class="p">,</span><span class="w"> </span><span class="nc">Settings</span><span class="p">(</span><span class="n">tmpdir</span><span class="p">).</span><span class="n">dir</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="nc">Settings</span><span class="p">(</span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'sub'</span><span class="p">,</span><span class="w"> </span><span class="s1">'bar'</span><span class="p">)).</span><span class="n">dir</span></code></pre>&#x000A;&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="new">new<span class="arg-list">(dir)</span></h2>&#x000A;&#x000A;<h4 id="creates-&lt;dir&gt;-if-does-not-exist,-and-its-parent-exists">creates &lt;dir&gt; if does not exist, and its parent exists</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">target</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">&#x000A;</span><span class="nc">Settings</span><span class="w"> </span><span class="n">target</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">target</span><span class="p">.</span><span class="n">exists</span><span class="w">&#x000A;&#x000A;</span><span class="n">target</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'bar'</span><span class="p">,</span><span class="w"> </span><span class="s1">'sub'</span><span class="p">)</span><span class="w">&#x000A;</span><span class="nc">Settings</span><span class="w"> </span><span class="n">target</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">target</span><span class="p">.</span><span class="n">exists</span></code></pre>&#x000A;&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when &lt;dir&gt; is not provided)</span></h3>&#x000A;&#x000A;<h4 id="uses-&quot;$howl_dir&quot;-when-specified">uses &quot;$HOWL_DIR&quot; when specified</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">getenv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">os.getenv</span><span class="w">&#x000A;  </span><span class="nb">os.getenv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nb">tostring</span><span class="w"> </span><span class="n">dir</span><span class="p">.</span><span class="n">path</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'HOWL_DIR'</span><span class="w">&#x000A;  </span><span class="nb">pcall</span><span class="w"> </span><span class="nc">Settings</span><span class="w">&#x000A;  </span><span class="nb">os.getenv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">getenv</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'system'</span><span class="p">).</span><span class="n">exists</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id="defaults-to-&quot;$home/.howl&quot;">defaults to &quot;$HOME/.howl&quot;</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">getenv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">os.getenv</span><span class="w">&#x000A;  </span><span class="nb">os.getenv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nb">tostring</span><span class="w"> </span><span class="n">dir</span><span class="p">.</span><span class="n">path</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s1">'HOME'</span><span class="w">&#x000A;  </span><span class="nb">pcall</span><span class="w"> </span><span class="nc">Settings</span><span class="w">&#x000A;  </span><span class="nb">os.getenv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">getenv</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'.howl'</span><span class="p">).</span><span class="n">exists</span></code></pre>&#x000A;&#x000A;</div>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="load_user">load_user<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="evaluates-one-of-{init.lua,-init.moon}-if-found,-in-that-order">evaluates one of {init.lua, init.moon} if found, in that order</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">init_moon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'init.moon'</span><span class="p">)</span><span class="w">&#x000A;</span><span class="n">init_moon</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'_G.__init_moon = true'</span><span class="w">&#x000A;&#x000A;</span><span class="n">settings</span><span class="o">\</span><span class="n">load_user</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">__init_moon</span><span class="w">&#x000A;&#x000A;</span><span class="n">_G</span><span class="p">.</span><span class="n">__init_moon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">&#x000A;</span><span class="n">init_lua</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'init.lua'</span><span class="p">)</span><span class="w">&#x000A;</span><span class="n">init_lua</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'_G.__init_lua = true'</span><span class="w">&#x000A;&#x000A;</span><span class="n">settings</span><span class="o">\</span><span class="n">load_user</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">__init_lua</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">__init_moon</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id="does-nothing-if-the-directory-is-not-set">does nothing if the directory is not set</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">has</span><span class="p">.</span><span class="n">no_error</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nc">Settings</span><span class="p">(</span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'sub'</span><span class="p">,</span><span class="w"> </span><span class="s1">'bar'</span><span class="p">))</span><span class="o">\</span><span class="n">load_user</span><span class="o">!</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id="raises-an-error-if-there-is-a-problem-loading-the-init-file">raises an error if there is a problem loading the init file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'init.moon'</span><span class="p">).</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'UGH!'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'UGH'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nc">Settings</span><span class="p">(</span><span class="n">tmpdir</span><span class="p">)</span><span class="o">\</span><span class="n">load_user</span><span class="o">!</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id="exposes-a-user_load-helper-for-loading-additional-files">exposes a user_load helper for loading additional files</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">init_lua</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'init.lua'</span><span class="p">)</span><span class="w">&#x000A;</span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'more.lua'</span><span class="p">).</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'return "more"'</span><span class="w">&#x000A;</span><span class="n">sub</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'ext'</span><span class="p">,</span><span class="w"> </span><span class="s1">'sub.lua'</span><span class="p">)</span><span class="w">&#x000A;</span><span class="n">sub</span><span class="p">.</span><span class="n">parent</span><span class="o">\</span><span class="n">mkdir</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">sub</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'return "sub"'</span><span class="w">&#x000A;</span><span class="n">init_lua</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sh">[[&#x000A;  _G.__loaded = user_load("more")&#x000A;  _G.__loaded_sub = user_load("ext/sub")&#x000A;]]</span><span class="w">&#x000A;&#x000A;</span><span class="n">settings</span><span class="o">\</span><span class="n">load_user</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s2">"more"</span><span class="p">,</span><span class="w"> </span><span class="n">_G</span><span class="p">.</span><span class="n">__loaded</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s2">"sub"</span><span class="p">,</span><span class="w"> </span><span class="n">_G</span><span class="p">.</span><span class="n">__loaded_sub</span></code></pre>&#x000A;&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="load_system">load_system<span class="arg-list">(name)</span></h2>&#x000A;&#x000A;<h4 id="returns-the-loaded-contents-of-a-file-named-system/&lt;name&gt;.lua">returns the loaded contents of a file named system/&lt;name&gt;.lua</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">sysdir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="s1">'system'</span><span class="w">&#x000A; </span><span class="n">sysdir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'foo.lua'</span><span class="p">).</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'return { a = "bar" }'</span><span class="w">&#x000A; </span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="ss">a:</span><span class="w"> </span><span class="s1">'bar'</span><span class="p">},</span><span class="w"> </span><span class="n">settings</span><span class="o">\</span><span class="n">load_system</span><span class="w"> </span><span class="s1">'foo'</span></code></pre>&#x000A;&#x000A;&#x000A;<h4 id="returns-nil-if-the-the-file-does-not-exist">returns nil if the the file does not exist</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">settings</span><span class="o">\</span><span class="n">load_system</span><span class="w"> </span><span class="s1">'no_such_file'</span></code></pre>&#x000A;&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="save_system">save_system<span class="arg-list">(name, table)</span></h2>&#x000A;&#x000A;<h4 id="saves-the-table-to-a-loadeable-file-system/&lt;name&gt;.lua">saves the table to a loadeable file system/&lt;name&gt;.lua</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><code><span class="n">settings</span><span class="o">\</span><span class="n">save_system</span><span class="w"> </span><span class="s1">'saved'</span><span class="p">,</span><span class="w"> </span><span class="ss">a:</span><span class="w"> </span><span class="s1">'bar'</span><span class="w">&#x000A;</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpdir</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="s1">'system/saved.lua'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="ss">a:</span><span class="w"> </span><span class="s1">'bar'</span><span class="p">},</span><span class="w"> </span><span class="nb">loadfile</span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="o">!</span></code></pre>&#x000A;&#x000A;</div>&#x000A;</div>
      <div class='footer text-muted'>
        <a href='/'>
          <img width="50" height="50" class="footer-logo" src="/images/howl.png" />
        </a>
        <div class='footer-follow'>
          <p>
            <a class='twitter-follow-button' data-lang='en' data-show-count='false' href='https://twitter.com/howleditor' rel='me'>
              Follow @howleditor
            </a>
          </p>
          <p>
            <a class='twitter-share-button' data-count='none' data-hashtags='howleditor' data-lang='en' data-text='The Howl Editor, a general purpose, light-weight customizable editor.' data-url='http://howl.io' href='https://twitter.com/share'>
              Tweet
            </a>
          </p>
        </div>
        <div class='footer-blurb'>
          <div>The Howl editor.</div>
          <div>
            Copyright 2012-2015
            <a class='alert-link' href='https://github.com/nilnor/howl/contributors'>
              The Howl Developers.
            </a>
          </div>
        </div>
      </div>
    </div>
    <script>
      <!-- / GA -->
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-45283282-1', 'howl.io');
      ga('send', 'pageview');
      <!-- / Twitter -->
      !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];
      if(!d.getElementById(id)){js=d.createElement(s);js.id=id;
      js.src="//platform.twitter.com/widgets.js";
      fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
    </script>
  </body>
</html>
